package ZooManagementSystem2.ZooCollection;

import ZooManagementSystem2.ZooEntity.User;
import ZooManagementSystem2.ZooEntity.UserKeeper;
import ZooManagementSystem2.ZooInterface.IDisplayInformation;
import ZooManagementSystem2.ZooInterface.IUserDataAccess;

import java.util.ArrayList;
import java.util.List;

public class UserCollection implements IDisplayInformation , IUserDataAccess {

    //该园区的饲养员
    private UserKeeper parkKeeper;
    //园区动物集合
    protected List<User> userList;

    //传全部参
    public UserCollection(UserKeeper parkKeeper, List<User> userList) {
        this.parkKeeper = parkKeeper;
        this.userList = userList;
    }
//    只传饲养员一个参
    public UserCollection(UserKeeper parkKeeper) {
        this.parkKeeper = parkKeeper;
        this.userList = new ArrayList<User>();

    }
//    只传集合一个参
    public UserCollection(List<User> userList) {
        this.userList = userList;
    }
//    不传参
    public UserCollection() {
        userList=new ArrayList<User>();
    }

    public UserKeeper getParkKeeper() {
        return parkKeeper;
    }

    public void setParkKeeper(UserKeeper parkKeeper) {
        this.parkKeeper = parkKeeper;
    }

    public List<User> getUserList() {
        return userList;
    }

    public void setUserList(List<User> userList) {
        this.userList = userList;
    }

    @Override
    public String displayAllUsers() {
        String displayMessageString="";
        for (User current:this.userList){
            displayMessageString+=current.getUserName();
        }
        return displayMessageString;
    }

    @Override
    public boolean addUser(User user) {
        return this.userList.add(user);
    }

    @Override
    public boolean deleteUser(User user) {
        return this.userList.remove(user);
    }

    @Override
    public boolean deleteUser(String userName) {
        return deleteUser(findUser(userName));
    }

    @Override
    public boolean updataUser(User oldUser, User newUser) {
        boolean updataUserSuccessed=false;
        if(oldUser==findUser(oldUser.getUserName())) {
            if(deleteUser(oldUser)){
                updataUserSuccessed=addUser(newUser);
            }
        }
        return updataUserSuccessed;
    }

    @Override
    public User findUser(String username) {
        User foundUser=null;
        for (User current:this.userList){
            if(current.getUserName().equalsIgnoreCase(username)){
                foundUser=current;
            }
        }
        return foundUser;
    }
}
